Skip to content

Avoid IR cycle for compiler-builtin ObjC typedefs shadowing themselves#3387

Merged
emilio merged 1 commit into
rust-lang:mainfrom
glandium:issue3386
Jul 7, 2026
Merged

Avoid IR cycle for compiler-builtin ObjC typedefs shadowing themselves#3387
emilio merged 1 commit into
rust-lang:mainfrom
glandium:issue3386

Conversation

@glandium

@glandium glandium commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

Objective-C's id/SEL/Class are compiler builtins, but SDK headers also redeclare them via a same-named typedef.

Under clang 22, asking such a typedef for its underlying type reports back the same declaration instead of desugaring to the written-out type. Treating that as an ordinary Alias creates a cycle once resolve_typerefs resolves the deferred reference, and nothing that computes canonical types checks for cycles, so it recurses forever - a stack overflow, or an infinite loop if the recursion gets tail-call-optimized.

Detect the same-USR case in CXType_Typedef handling and resolve directly as the inner kind instead of aliasing to it, so the cycle is never constructed.

Adds a self-contained regression test (the typedef lives in an auxiliary header included via -isystem, so it doesn't need a real macOS SDK) that segfaults on main and passes with this fix.

Fixes #3386

Comment thread bindgen/ir/ty.rs
// resolve directly as the inner kind instead of
// aliasing to it, exactly as we would if `ty` itself had
// reported that kind.
let same_underlying_decl = matches!(

@emilio emilio Jul 3, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to check the USR tho? Can we check cursor == inner.declaration() or so?

View changes since the review

@glandium glandium Jul 3, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, cursor == inner.declaration() is false for id/Class even in the exact case we want to catch, and cursor.canonical() == inner.declaration().canonical() is also false. Only the USR matches. Looks like the "builtin definitions" cursor libclang hands back for the inner type isn't the same node as the source-level typedef decl, just the same USR.

@glandium glandium requested a review from emilio July 7, 2026 00:01

@emilio emilio left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I dug a bit and couldn't come up with a simpler alternative.

View changes since this review

@emilio emilio enabled auto-merge July 7, 2026 08:54
@emilio

emilio commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

@glandium can you rebase? The ci issue should be fixed now.

Objective-C's id/SEL/Class are compiler builtins, but SDK headers also
redeclare them via a same-named typedef.

Under clang 22, asking such a typedef for its underlying type reports
back the same declaration instead of desugaring to the written-out type.
Treating that as an ordinary Alias creates a cycle once resolve_typerefs
resolves the deferred reference, and nothing that computes canonical
types checks for cycles, so it recurses forever - a stack overflow, or
an infinite loop if the recursion gets tail-call-optimized.

Detect the same-USR case in CXType_Typedef handling and resolve directly
as the inner kind instead of aliasing to it, so the cycle is never
constructed.

Adds a self-contained regression test (the typedef lives in an auxiliary
header included via -isystem, so it doesn't need a real macOS SDK) that
segfaults on main and passes with this fix.

Fixes rust-lang#3386
auto-merge was automatically disabled July 7, 2026 09:32

Head branch was pushed to by a user without write access

@rustbot

rustbot commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@emilio emilio enabled auto-merge July 7, 2026 09:34
@emilio emilio added this pull request to the merge queue Jul 7, 2026
Merged via the queue into rust-lang:main with commit 8f52063 Jul 7, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ObjC id/SEL/Class typedef shadowing causes infinite recursion under clang 22

3 participants